package com.bl.student.api.controller.apply;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.service.biz.apply.ApplyService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.apply.ApplyAddVo;
import com.bl.student.service.vo.apply.ApplyAgreeVo;
import com.bl.student.service.vo.apply.ApplyListVo;
import com.bl.student.service.vo.apply.ApplyModifyMoneyVo;
import com.bl.student.service.vo.chat.RoomAddVo;
import com.bl.student.service.vo.user.RoomUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by liangweikang on 18/1/29.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis/apply"})
@Api(value = "申请", description = "申请相关api")
public class ApplyController extends BaseController {
    @Autowired
    ApplyService applyService;

    @ApiOperation(value = "根据taskid获取发布任务的申请列表 登录", notes = "乙方申请记录 ")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "乙方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "taskid", value = "任务id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, paramType = "query", dataType = "int", defaultValue = "10")})

    @GetMapping(value = {"/get_list_login"})
    public ApiResponseBody<ApplyListVo> getLists(
//            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "taskid", required = true) String taskid,
            @RequestParam(value = "status", required = true) int status,
            @RequestParam(value = "page", required = true, defaultValue = "1") int page,
            @RequestParam(value = "size", required = true, defaultValue = "10") int size,
            HttpServletRequest request) {
        Pageable pageable = new PageRequest(page - 1, size);
        ApplyListVo applyListVo = applyService.getTaskApplyLists(taskid, pageable, getUserInfo(request), status);

        return ApiResponseBody.createSuccessBody(applyListVo);
    }

    @ApiOperation(value = "根据taskid获取发布任务的申请列表 非登录", notes = "发布任务申请记录 ")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "partyaid", value = "甲方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "taskid", value = "任务id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, paramType = "query", dataType = "int", defaultValue = "10")})

    @GetMapping(value = {"/get_task_list"})
    public ApiResponseBody<ApplyListVo> getTaskLists(
//            @RequestParam(value = "partyaid", required = true) String partyaid,
            @RequestParam(value = "taskid", required = true) String taskid,
            @RequestParam(value = "status", required = true) int status,
            @RequestParam(value = "page", required = true, defaultValue = "1") int page,
            @RequestParam(value = "size", required = true, defaultValue = "10") int size,
            HttpServletRequest request) {
        Pageable pageable = new PageRequest(page - 1, size);
        ApplyListVo applyListVo = applyService.getTaskApplyLists(taskid, pageable, getUserInfo(request), status);

        return ApiResponseBody.createSuccessBody(applyListVo);
    }

    @ApiOperation(value = "乙方申请加入任务、状态信息：0-已申请，1-已接受 2- 已拒绝", notes = "增加申请加入任务的信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/applyin"})
    public ApiResponseBody applyin(
            @Valid @RequestBody ApplyAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
//        String userid = "80bf42ac3abd47d39c7af5f9da6c19fb";
        applyService.apply(body,getUserInfo(request).getId());

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "任务状态 PRE -> ONLINE 甲方选择某个申请人为任务的执行者：0-已申请，1-已接受 2- 已拒绝 ", notes = "甲方选择申请人")
    @ApiImplicitParams({})
    @PostMapping(value = {"/agree"})
    public ApiResponseBody agree(
            @Valid @RequestBody ApplyAgreeVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        applyService.agree(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "任务状态 PRE -> ONLINE  群招强制变为ONLINE ", notes = "甲方选择申请人")
    @ApiImplicitParams({})
    @PostMapping(value = {"/online"})
    public ApiResponseBody forceOnline(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        applyService.forceOnline(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "发起聊天 ", notes = "甲方选择申请人")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_room"})
    public ApiResponseBody addRoom(
            @Valid @RequestBody RoomAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        applyService.addRoom(body);

        return ApiResponseBody.createSuccessBody(null);
    }



    @ApiOperation(value = "修改报价 ", notes = "修改报价")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify_money"})
    public ApiResponseBody modifyMoney(
            @Valid @RequestBody ApplyModifyMoneyVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        applyService.modifyMoney(body.getApplyId(), body.getMoney());

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "获取聊天室", notes = "获取聊天室 ")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "partyaid", value = "甲方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "partyaHxName", value = "甲方环信name", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "partybHxName", value = "乙方环信name", required = true, paramType = "query", dataType = "String")})

    @GetMapping(value = {"/get_room"})
    public ApiResponseBody<ApplyListVo> getRoom(
//            @RequestParam(value = "partyaid", required = true) String partyaid,
            @RequestParam(value = "taskId", required = true) String taskId,
            @RequestParam(value = "partyaHxName", required = true) String partyaHxName,
            @RequestParam(value = "partybHxName", required = true, defaultValue = "1") String partybHxName,
            HttpServletRequest request) {
        String roomId = applyService.getRoom(partyaHxName, partybHxName, taskId);

        return ApiResponseBody.createSuccessBody(roomId);
    }

    @ApiOperation(value = "获取聊天室", notes = "获取聊天室 ")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "partyaid", value = "甲方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roomId", value = "", required = true, paramType = "query", dataType = "String")})

    @GetMapping(value = {"/get_by_room"})
    public ApiResponseBody<ApplyListVo> getUserInfo(
//            @RequestParam(value = "partyaid", required = true) String partyaid,
            @RequestParam(value = "roomId", required = true) String roomId,
            HttpServletRequest request) {
        RoomUserInfo roomUserInfo = applyService.getUserInfoByRoom(roomId);

        return ApiResponseBody.createSuccessBody(roomUserInfo);
    }


}
